package com.imooc.web;

import com.imooc.enums.YesOrNo;
import com.imooc.pojo.Carousel;
import com.imooc.pojo.Category;
import com.imooc.service.CarouselService;
import com.imooc.service.CategoryService;
import com.imooc.service.ItemService;
import com.imooc.utils.Result;
import com.imooc.vo.CategoryVo;
import com.imooc.vo.NewItemsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <h1></h1>
 *
 * @author : dzj
 * @create: 2022-02-17 08:15
 */
@RestController
@RequestMapping("/index")
@Api(value = "首页",tags = {"首页接口"})
public class IndexController {
    @Autowired
    private CarouselService carouselService;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ItemService itemService;
    @ApiOperation(value = "首页轮播图", notes = "首页轮播图",httpMethod = "GET")
    @GetMapping("/carousel")
    public Result carousel() {
        List<Carousel> carousels = carouselService.queryAll(YesOrNo.YES.getCode());
        return Result.ok(carousels);
    }

  @ApiOperation(value = "一级分类",httpMethod = "GET")
    @GetMapping("/cats")
    public Result cats() {
      List<Category> categories = categoryService.queryAllRootLevelCat();
      return Result.ok(categories);
    }

    @ApiOperation(value = "获取商品子分类",httpMethod = "GET")
    @GetMapping("/subCat/{rootCatId}")
    public Result subCat(@PathVariable(name = "rootCatId",required = true) @ApiParam(name = "rootCatId", value = "以及分类id",required = true) Integer rootCatId) {
        if(rootCatId==null) {
            return Result.errorMsg("分类不存在");
        }
        List<CategoryVo> subCatList = categoryService.getSubCatList(rootCatId);
        return Result.ok(subCatList);
    }


    @ApiOperation(value = "查询每个以及分类下的最新6条商品数据",httpMethod = "GET")
    @GetMapping("/sixNewItems/{rootCatId}")
    public Result sixNewItems(@PathVariable(name = "rootCatId",required = true) @ApiParam(name = "rootCatId", value = "以及分类id",required = true) Integer rootCatId) {
        if(rootCatId==null) {
            return Result.errorMsg("分类不存在");
        }

        List<NewItemsVo> sixNewItemLazy = categoryService.getSixNewItemLazy(rootCatId);

        return Result.ok(sixNewItemLazy);
    }

}
